package com.brozha.ans_sys.controller;


import com.brozha.ans_sys.entity.User;
import com.brozha.ans_sys.exception.AnsSysException;
import com.brozha.ans_sys.service.UserService;
import com.brozha.ans_sys.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author brozha
 * @since 2021-04-04
 */
@RestController
@RequestMapping("/api/user")
@Api(tags = "用户api")
@CrossOrigin
@Slf4j
public class UserController {

    @Autowired
    UserService userService;

    @GetMapping("/findAll")
    @ApiOperation("查找全部")
    public List<User> findAll(){

        return userService.list();
    }

    /**
     * 用户注册
     * @param username
     * @param userpwd
     * @return
     */
    @PostMapping("/regist")
    @ApiOperation("用户注册")
    public Result regist(@RequestParam("username")String username,
                         @RequestParam("userpwd")String userpwd,
                         @RequestParam("nickname")String nickname,
                         @RequestParam(value = "avatarUrl",defaultValue = "https://ans-sys.oss-cn-chengdu.aliyuncs.com/avatar/default/defaulAvatar.jpg")String avatarUrl){
        log.info("发送注册请求");
        return userService.regist(username,userpwd,nickname,avatarUrl);
    }

    /**
     * 用户登录
     * @param username
     * @param userpwd
     * @return
     */
    @GetMapping("/login")
    @ApiOperation("用户登录")
    public Result login(@RequestParam("username")String username,
                        @RequestParam("userpwd")String userpwd){
        return userService.login(username,userpwd);
    }

    @GetMapping("/info")
    @ApiOperation("获取用户信息")
    public Result getInfo(HttpServletRequest request){
        String token = request.getHeader("token");
        return userService.getInfo(token);
    }

    @PutMapping("/info")
    @ApiOperation("获取用户信息")
    public Result updateUser(HttpServletRequest request,
                         @RequestParam("userpwd")String userpwd,
                         @RequestParam("nickname")String nickname,
                         @RequestParam(value = "avatarUrl",defaultValue = "https://ans-sys.oss-cn-chengdu.aliyuncs.com/avatar/default/defaulAvatar.jpg")String avatarUrl){
        String token = request.getHeader("token");
        return userService.updateUser(token,userpwd,nickname,avatarUrl);
    }

    @DeleteMapping("/logout")
    @ApiOperation("退出登录")
    public Result logOut(HttpServletRequest request){
        String token = request.getHeader("token");
        return userService.logOut(token);
    }




}

